<?php
//define('FPDF_FONTPATH', 'font/');
require('fpdf.php');

class reportProduct extends FPDF
{
  var $widths;
  var $aligns;

function SetWidths($w)
{
  $this->widths=$w;
}

function SetAligns($a)
{
  $this->aligns=$a;
}
function Row($data,$rec=true,$color=false,$kolom='',$kolom1='',$kolom2='')
{
  $nb=0;
  for($i=0;$i<count($data);$i++)
    $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
  $h=(5.5*$nb);
  $this->CheckPageBreak($h);
  for($i=0;$i<count($data);$i++)
  {
   $w=$this->widths[$i];
   $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
   $x=$this->GetX();
   $y=$this->GetY();
   ($color==false)? $this->SetTextColor(0):
   ($kolom==$i || $kolom1==$i || $kolom2==$i)?
    $this->SetTextColor(255,0,0): $this->SetTextColor(0);
   ($rec==true)? $this->Rect($x,$y,$w,$h+2):'';
   $this->MultiCell($w,6,$data[$i],'',$a);
   $this->SetXY($x+$w,$y);
  }
  $this->Ln($h+2);
}

function CheckPageBreak($h)
{
  if($this->GetY()+$h>$this->PageBreakTrigger)
  $this->AddPage($this->CurOrientation);
}

function NbLines($w,$txt)
{
  $cw=&$this->CurrentFont['cw'];
  if($w==0)
   	$w		=$this->w-$this->rMargin-$this->x;
  	$wmax	=($w-2*$this->cMargin)*1000/$this->FontSize;
  	$s		=str_replace("\r",'',$txt);
  	$nb		=strlen($s);
  if($nb>0 and $s[$nb-1]=="\n")
   	  $nb--;
	  $sep=-1;
	  $i=0;
	  $j=0;
	  $l=0;
	  $nl=1;
  while($i<$nb){
   $c=$s[$i];
   if($c=="\n") {
		$i++;
		$sep=-1;
		$j=$i;
		$l=0;
		$nl++;
		continue;
   }
   if($c==' ')
    $sep=$i;
   $l+=$cw[$c];
   if($l>$wmax) {
    if($sep==-1) {
     if($i==$j)
      $i++;
    }
    else
     $i=$sep+1;
    $sep=-1;
    $j=$i;
    $l=0;
    $nl++;
   }
   else
   $i++;
 }
 return $nl;
}

function Header()
{
  $zn=new zetro_manager();
  
  if($this->kriteria=='single')
  {
	   $this->Ln(2);
	   $this->Image(base_url().'asset/img/rutilahu.jpg',10,6,50,15);
	   $this->Ln(5);
	   $this->SetFont('Arial','B','14');
	   $this->Cell(200,5,$this->nama,0,1,'C');
	   $this->ln(4);
	   $this->Cell(185,0.2,'',1,1,'C');
	   $this->ln(4);
  }
  if($this->kriteria=='all'){
	   $this->Ln(2);
	   $this->Image(base_url().'asset/img/rutilahu.jpg',10,6,50,15);
	   $this->Ln(2);
	   $this->SetFont('Arial','B','14');
	   $this->Cell(270,5,$this->nama,0,1,'C');
	   $this->ln(4);
	   ($this->filter=='legal')?
     $this->Cell(325,0.15,'',1,1,'C'):$this->Cell(270,0.15,'',1,1,'C');
	   $this->ln(4);
     $this->Cell(10,8,'Kecamatan : '.$this->refer,0,1,'L');
			// set nama header tabel transaksi
	   $this->SetFont('Arial','B','12');
			  $this->Ln(2);
			  $this->SetFillColor(225,225,225);
			  $kol=$zn->Count($this->section,$this->nfilex);
			  //$this->Cell(10,8,'No.',1,0,'C',true);
			  $data=array();$width=array();$algn=array();
			  for ($i=1;$i<=$kol;$i++){
				  $d=explode(',',$zn->rContent($this->section,$i,$this->nfilex));
				  $data[]	=$d[0];
				  $width[]	=$d[2];
				  $align[]	='C';
				  //$this->Cell($d[2],8,$d[0],1,0,'C',true);
			  }
			  $this->SetWidths($width);
			  $this->SetAligns($align);
			  $this->Row($data);
 }
}

function Footer(){
 // Position at 1.5 cm from bottom
  ($this->kriteria=='single')?$this->SetY(-30):$this->SetY(-15);
  //Arial italic 8
  $this->SetFont('Arial','i',7);
  if($this->kriteria=='faktur'){
  $this->Cell(150,10,'Berlaku sebagai faktur pajak sesuai Peraturan Menkeu No. 38/PMK.03/2010',0,0,'L');
  }else if($this->kriteria=='single'){
  //print_r($this->log);
    $this->Cell(0,0.15,'',1,1,'C');
    $this->Cell(10,5,'Riwayat Data :',0,1,'L');
    foreach($this->log as $l)
    {
     $drbh='';$drbh_tgl='';$drbh_by='';
      if($l['stat_log']=='Dibuat')
       {
       $this->Cell(10,5,'   '.$l['stat_log'].': '.$l['stat_date'].' ['.$l['created_by'].' ]',0,1,'L');     
       }else{
        $drbh=$l['stat_log'];
        $drbh_tgl=$l['stat_date'];
        $drbh_by=$l['created_by'];
       }
       
      $n=$l['total'];
     }
    ($drbh!='')?$this->Cell(10,5,'   '.$drbh.': '.$drbh_tgl.' ['.$drbh_by.']',0,1,'L'):'';
    $this->Cell(10,5,'   Total revisi :'.$n,0,1,'L');

  }else{
  $this->Cell(0,10,'Print Date :'.date('d F Y'),0,0,'C');
  }
  $this->Cell(0,10,'Page '.$this->PageNo().' of {nb}',0,0,'R');
}
//public function

public function setLog($log)
{
  $this->log=$log;
}

public function getLog()
{
  return $this->log;
}
public function setKriteria($i){
  $this->kriteria=$i;
}

public function getKriteria(){
  return $this->kriteria;
}

public function setNoFaktur($n){
 $this->nofaktur=$n;	
}

public function getNoFaktur(){
  return $this->nofaktur;
}
public function setNama($n){
  $this->nama=$n;
}
public function getNama(){
  return $this->nama;
}
public function setReferer($n){
  	$this->refer=$n;
 
}
public function getReferer(){
  $this->refer;
}

public function setSection($n){
  $this->section=$n;
}

public function getSection(){
  return $this->section;
}
public function setFilter($n){
	$this->filter=$n;
}
public function getFilter(){
	return $this->filter;
}
public function setDataset($n){
  $this->dataset=$n;
}

public function setFilename($n){
	$this->nfilex=$n;
}
public function getFilename(){
	return $this->nfilex;
}
public function getDataset(){
  return $this->dataset;
}

}

?>